define(function(require, exports, module) {
	J = jQuery;
    require('autovalidate');
    require('jqueryform');
	require('layer');
	layer.config({
	    path: '/assets/js/vendors/layer/' //layer.js所在的目录，可以是绝对目录，也可以是相对目录
	});

	//===========================================登录开始=================================================
    /*验证码重新加载*/
    $("#reloadCaptcha").click(function(){
        new_src = '/captcha/default?'+Math.random();
        $(this).find("img").attr("src",new_src);
    });

    $("#myloginForm").ajaxForm({
        dataType: "json"
        , beforeSubmit: function(formData, jqForm, options) {}
        , success: function(ret) {
            if (ret.code == 0) {
                location.href = "/admin/index";
            } else {
                showFailMsg(ret.message);
            }
        }
        , error: function(ret) {alert(ret.message);}
        , complete: function(ret) {} 	      // 无论是 success 还是 error，最终都会被回调
    });

	//用户登录名
	$("#login_username").on("blur", function(){
		var username=$("#login_username").val();
		if(username.length>0){
			if(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	

    
    /*用户表单输入时删除错误提示*/
    $("body").delegate("form input","keydown",function(){
        $(this).parents(".form-group").removeClass("has-error");
        $(this).next(".help-block").remove();
    });
    
	//用户密码
	$("#login_password").on("blur", function(){
		var password=$("#login_password").val();
		if(password.length>=6){
			if(/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$(this).closest('div').removeClass('has-error').addClass('has-success');
			}else{
				$(this).closest('div').removeClass('has-success').addClass('has-error');
			}
		}else{
			$(this).closest('div').removeClass('has-success').addClass('has-error');
		}
	});
	
	$("#login-btn").on("click", function(){
		var username=$("#username").val();
		var password=$("#password").val();
		var captcha=$("#captcha").val();
		var redirectUrl=$("#redirectUrl").val();
		var rememberMe=$("#login_rememberMe").val();
		
		if(username.length>0){
			if(!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#username").closest('div').removeClass('has-success').addClass('has-error');
				$("#username").focus();
				return false; 
			}
		}else{
			$("#login_username").closest('div').removeClass('has-success').addClass('has-error');
			$("#login_username").focus();
			return false; 
		}
		
		if(password.length>=6){
			if(!/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$("#password").closest('div').removeClass('has-success').addClass('has-error');
				$("#password").focus();
				return false; 
			}
		}else{
			$("#password").closest('div').removeClass('has-success').addClass('has-error');
			$("#password").focus();
			return false;
		}
		//layer.msg("11111", {icon: 5});
		$.ajax({
    		url: '/member/login.json', 
    		data: {'username': username,'password': password,'redirectUrl':redirectUrl,'captcha': captcha,'rememberMe':$("#rememberMe").is(':checked')},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(data){	
    			if(data.code==0){
    				window.location.href = data.url;
    				return false;
    			}else{
    				layer.msg(data.message, {icon: 2});
					return false;
				}
          	}
    	});
		return false;
	});
	$("#login-form").keydown(function(e){
		 var e = e || event,
		 keycode = e.which || e.keyCode;
		 if (keycode==13) {
		  $(".log_btn").trigger("click");
		 }
	});
	//===========================================登陆结束=================================================

});